﻿using System;
using Myotragus.Data.Containers;
using Myotragus.Data.Domain;
using Myotragus.Data.Metadata;

namespace Myotragus.Data.Hbm
{
	public class DefaultsSaveEntityHbmGenerator : LocalizableEntityHbmGenerator,
		IDefaultsSaveEntityHbmGenerator
	{
		public DefaultsSaveEntityHbmGenerator(IEntitiesContainer entities, 
			ILocalizableEntityMapping mapping)
			: base(entities, mapping)
		{
		
		}

		protected const string StrLocalizableFieldFormat = @"
	<many-to-one name=""{0}"" column=""{1}"" cascade=""all-delete-orphan""
		class=""Myotragus.Data.Domain.DefaultsSaveLocalizableText, Myotragus.Data""/>
";

		protected override string GenerateLocalizableField(IPropertyMapping p)
		{
			return string.Format(StrLocalizableFieldFormat, p.PropertyName, p.ColumnName);
		}

		protected override Type ResolveRersistentType(Type interfaceType)
		{
			return Entities.ResolveType(
				typeof(IDefaultsSaveEntity<>).MakeGenericType(interfaceType));
		}
	}
}